import time
from selenium.webdriver.common.by import By
from Base.base_action import BaseAction


class SearchProduct(BaseAction):
    search_pro = (By.XPATH, "//*[contains(@text, '查产品')]")
    search_type = (By.CLASS_NAME, "android.widget.LinearLayout")
    input_text = (By.ID, "org.c2h4.afei.beauty:id/et_container")
    opt = (By.XPATH, "//*[contains(@text, '调试')]")
 
    # 移动调试按钮
    def move_opt(self):
        time.sleep(5)
        opt_loc = self.find_element(self.opt)
        position = opt_loc.location
        self.move(loc=opt_loc, x1=None, y1=None, x2=position["x"]+30, y2=position["y"]+200, duration=3000)

    # 点击查产品按钮
    def click_search_button(self):
        try:
            self.move_opt()
        except:
            pass
        self.click_element(self.search_pro)
        time.sleep(1)

    # 查找全部
    def search_all(self, search_word):
        self.find_elements(self.search_type)[0].click()
        self.click_element(self.input_text)
        self.input_keys(self.input_text, search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        txt = self.get_page_source()
        assert search_word in txt, "搜索内容不在页面中， 评论失败"  # 断言评论是否成功
        self.clear_words(self.input_text)

    # 查找文章
    def search_article(self, search_word):
        self.find_elements(self.search_type)[1].click()
        self.click_element(self.input_text)
        self.input_keys(self.input_text, search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        txt = self.get_page_source()
        assert search_word in txt, "搜索内容不在页面中， 评论失败"  # 断言评论是否成功
        self.clear_words(self.input_text)

    # 查找讨论
    def search_discuss(self, search_word):
        self.find_elements(self.search_type)[2].click()
        self.click_element(self.input_text)
        self.input_keys(self.input_text, search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        txt = self.get_page_source()
        assert search_word in txt, "搜索内容不在页面中， 评论失败"  # 断言评论是否成功
        self.clear_words(self.input_text)

    # 查找产品
    def search_product(self, search_word):
        self.find_elements(self.search_type)[3].click()
        self.click_element(self.input_text)
        self.input_keys(self.input_text, search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        txt = self.get_page_source()
        assert search_word in txt, "搜索内容不在页面中， 评论失败"  # 断言评论是否成功
        self.clear_words(self.input_text)

    # 查找成分
    def search_ingredient(self, search_word):
        self.find_elements(self.search_type)[4].click()
        self.click_element(self.input_text)
        self.input_keys(self.input_text, search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        txt = self.get_page_source()
        assert search_word in txt, "搜索内容不在页面中， 搜索失败"  # 断言评论是否成功
        self.clear_words(self.input_text)

    # 集成每种查找方式
    def search_test(self, sw):
        self.search_all(sw)
        self.search_article(sw)
        self.search_discuss(sw)
        self.search_product(sw)
        self.search_ingredient(sw)
